package mini.ideashare.cms.manager;

import mini.ideashare.cms.dao.ArticleDAO;
import mini.ideashare.cms.model.Article;
import mini.ideashare.cms.model.qc.ArticleQC;
import mini.ideashare.cms.model.qc.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ArticleManager {

    @Autowired
    private ArticleDAO articleDAO;

    public List<Article> queryArticle(Article article, Integer pageIndex , Integer pageSize){
        Page page = new Page(pageIndex,pageSize);
        ArticleQC qc  = new ArticleQC();
        qc.setPage(page);
        BeanUtils.copyProperties(article,qc);
        List<Article> articles = articleDAO.listArticle(qc);
        return articles;
    }

    public Long countArticle(Article article){
        ArticleQC qc  = new ArticleQC();
        BeanUtils.copyProperties(article,qc);
        Long count = articleDAO.countArticle(qc);
        return count;
    }

    public int saveArticle(Article article){

        if(article.getId()!=null){
            return articleDAO.updateArticle(article);
        }else {
            article.setCreateTime(new Date());
            return articleDAO.insertArticle(article);
        }

    }
    public int deleteArticle(Long id){

        return articleDAO.deleteArticleById(id);

    }



}
